<?php
namespace Admin\Controller;
use Think\Controller;

class MenuController extends BaseController
{
	public function indexAction() 
    {
		//分类树
		vendor('Phpcms.Tree', '', '.class.php');
		$Tree = new \Tree();
		$Tree->icon = array('&nbsp;&nbsp;&nbsp;│ ','&nbsp;&nbsp;&nbsp;├─ ','&nbsp;&nbsp;&nbsp;└─ ');
		$Tree->nbsp = '&nbsp;&nbsp;&nbsp;';
		
		$Menu = M('Menu');
		$menu_list = $Menu->order('list_order asc, id asc')->select();
		foreach ($menu_list as $key => $val) {
		    $menu_list[$key]['is_on'] = $val['is_on'] == 1 ? '' :"<span class='cRed'>否</span>";
			$menu_list[$key]['str_manage'] = '<a href="' . U("Menu/add", array('parent_id' => $val['id'])) . '">添加子菜单</a> | <a href="' . U("Menu/update", array('id' => $val['id'])) . '">修改</a> | <a href="' . U('Menu/delete', array('id' => $val['id'])) . '">删除</a>';
		}
		$str  = "<tr>
					<td align='center'><input name='list_order[\$item[id]]' type='text' size='3' value='\$item[list_order]' class='input-text-c input-text'></td>
					<td align='center'>\$item[id]</td>
					<td>\$spacer\$item[chinese_name]</td>
					<td align='center'>\$item[is_on]</td>
					<td align='center'>\$item[str_manage]</td>
				</tr>";
		$Tree->init($menu_list);
		$menu_list = $Tree->get_tree(0, $str);		
		$this->assign('menu_list', $menu_list);
		$this->display();	
	}	
	
	public function addAction() 
    {
		if (IS_POST) {
			$Menu = D('Menu');
			if ($Menu->create($_POST['info'])) {
				if (false !== $Menu->add()) {
					$this->success('添加成功！');
				} else {
					$this->error('添加失败!');
				}
			} else {
				$this->error($Menu->getError()); 
			}
		} else {
			$parent_id = isset($_GET['parent_id']) ? intval($_GET['parent_id']) : 0;
			
			//分类树
			vendor('Phpcms.Tree', '', '.class.php');
			$Tree = new \Tree();
		
			$Menu = M('Menu');
			$menu_list = $Menu->order('list_order asc, id asc')->select();
			foreach ($menu_list as $key => $val) {
				$menu_list[$key]['selected'] = $val['id'] == $parent_id ? 'selected' : '';
			}

			$str  = "<option value='\$item[id]' \$item[selected]>\$spacer \$item[chinese_name]</option>";
			$Tree->init($menu_list);
			$menu_list = $Tree->get_tree(0, $str);		
			$this->assign('menu_list', $menu_list);
			$this->display();
		}
	}
	
	public function updateAction() 
    {
		if (IS_POST) {
			$Menu = D('Menu');
			if ($Menu->create($_POST['info'])) {
				if (false !== $Menu->save()) {
					$this->success('修改成功！', U('Menu/index'));
				} else {
					$this->error('修改失败!');
				}
			} else {
				$this->error($Menu->getError()); 
			}
		} else {
            $id = I('get.id', 0, 'intval');
			$Menu = M('Menu');
			$menu = $Menu->where('id = ' . $id)->order('list_order asc, id asc')->find();
			
			//分类树
			vendor('Phpcms.Tree', '', '.class.php');
			$Tree = new \Tree();
			$menu_list = $Menu->select();
			foreach ($menu_list as $key => $val) {
				$menu_list[$key]['selected'] = $val['id'] == $menu['parent_id'] ? 'selected' : '';
			}
			$str  = "<option value='\$item[id]' \$item[selected]>\$spacer \$item[chinese_name]</option>";
			$Tree->init($menu_list);
			$menu_list = $Tree->get_tree(0, $str);		
			
			$this->assign('menu_list', $menu_list);
			$this->assign('menu', $menu);
			$this->display();
		}
	}
	
	public function deleteAction() 
    {
        $id = I('get.id', 0, 'intval');
        $Menu = M('Menu');
        $list = $Menu->where(array('parent_id' => $id))->select();
        if (!empty($list)) {
            $this->error('该菜单下有子菜单，请先删除子菜单！');
        }
        
		$res = M('Menu')->delete($id);
		if (false !== $res) {
			$this->success('删除成功！', U('Menu/index'));	
		} else {
			$this->error('删除失败!');	
		}
	}
	
	public function listOrderAction()
	{
		$Menu = D('Menu');
		$arr_list_order = $_POST['list_order'];
		foreach ($arr_list_order as $key => $val) {
			$data = array('list_order' => intval($val));
			$Menu ->where('id = ' . intval($key))->setField($data);
		}
		$this->redirect('index');
	}
}